define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall_parents_order/index',
                    multi_url: 'mall_parents_order/multi',
                    table: 'mall_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                showExport: false,
                searchFormVisible: false, //是否始终显示搜索表单
                columns: [
                    [
                        //目前只展示自己菜单，一对多关联展示为一对一，到时候前端做得改
                        {checkbox: true},
                        {field: 'id', title: __('ID'),operate:false},
                        {field: 'sn', title: __('Sn')},
                        {field: 'type', title: __('Order_type'), searchList: {"1":__('Order_type 1'),"2":__('Order_type 2'),"3":__('Order_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'mallordergoods.id', title: __('子订单ID'),operate:false},
                        {field: '', title: __('Title'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'"><img width=30px" src="'+data.mallordergoods.goods_image+'">'+data.mallordergoods.goods_name+'<br>'+data.mallordergoods.type_txt+'</div>'
                                return html
                            }},
                        {field: '', title: __('Unit_price_score'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.mallordergoods.unit_price+'</div>'
                                return html
                            }},
                        {field: 'mallordergoods.goods_spec_name', title: __('Goods_spec_name'),operate:false},
                        {field: 'mallordergoods.buy_nums', title: __('Buy_nums'),operate:false},
                        {field: '', title: __('User_info'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.user.username+'<br>'+data.user.mobile+'</div>'
                                return html
                            }},
                        {field: '', title: __('Pay_type'),
                            formatter: function(row,data){
                                console.log(row,data)
                                let html = '<div data-sn="'+data.sn+'" data-id="'+data.id+'">'+data.pay_type_txt+'：'+data.mallordergoods.unit_price+'</div>'
                                return html
                            }},
                        {field: 'pay_amount', title: __('Pay_amount'),operate:false},
                        {field: 'status', title: __('父状态'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5'),"10":__('Status 10')}, formatter: Table.api.formatter.normal,visible:false},
                        {field: 'mallordergoods.status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.normal},
                        {
                            field: 'buttons',
                            title: '',
                            table: table,
                            events: {
                                'click .btn-duplicate1': function (e, value, row) {
                                    Layer.prompt({
                                        title: "请填写审核备注",
                                        success: function (layero) {
                                            $("input", layero).prop("placeholder", "备注");
                                        }
                                    }, function (value) {
                                        Fast.api.ajax({
                                            url: "mall_parents_order/update_remark/ids/" + row.id,
                                            data: {admin_note: value, status: "1"},
                                        }, function (data, ret) {
                                            Layer.closeAll();
                                            $(".btn-refresh").trigger("click");
                                            //return false;
                                        });
                                    });
                                    return false;
                                },
                            },
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('查看父订单详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_parents_order/detail/ids/{ids}',
                                    extend: 'data-area=["100%","100%"]',
                                },
                                {
                                    name: 'ajax',
                                    text: __('父订单批量发货'),
                                    title: __('父订单批量发货'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_parents_order/delivery/ids/{ids}',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.type == "1" && row.status == "6" && row.mallordergoods.type != "2"){
                                            return true;
                                        }
                                        else if(row.type == "1" && row.status == "2" && row.mallordergoods.type != "2"){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name: 'success',
                                    text: '父订单备注',
                                    classname: 'btn btn-xs btn-warning btn-duplicate1',
                                    icon: 'fa fa-copy',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        //if(row.mallordergoods.status == "2")
                                        //    return false;
                                        //else
                                            return true;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('子订单一键发货'),
                                    title: __('子订单一键发货'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_parents_order/delivery_child/id/{row.mallordergoods.id}',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.type == "1" && row.mallordergoods.status == "2" && row.mallordergoods.type != "2")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('查看物流'),
                                    title: __('查看物流'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_parents_order/delivery_child/id/{row.mallordergoods.id}',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.mallordergoods.status == "3" && row.mallordergoods.type != "2")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('一键核销'),
                                    title: __('一键核销'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    // icon: 'fa fa-magic',
                                    url: 'mall_parents_order/verification/id/{row.mallordergoods.id}',
                                    extend: 'data-area=["50%","50%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.type == "2" && row.mallordergoods.status == "2")
                                            return true;
                                        else
                                            return false;
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                    ]
                ],
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    console.log(Config.ids);
                    if(Config.ids != null)
                    {
                        filter.order_id = Config.ids;     // 设置值，type为字段名
                        op.order_id = "=";      // 设置条件
                        params.filter = JSON.stringify(filter);
                        params.op = JSON.stringify(op);
                    }
                    return params;
                }
            });

            $(document).on('click','.btn_sn' ,function (e) {
                console.log( $(this).data('sn'))
                Layer.open({
                    type: 2,
                    content:'goods/purchaseDetail?sn='+$(this).data('sn')+'&ids='+$(this).data('id'),
                    area:['100%','100%'],
                    //content: Config.url
                })
            })

//绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    console.log(panel.attr("id"))
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
//必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        detail: function () {
            $(document).on('click','.delivery_child' ,function (e) {
                console.log( $(this).data('id'))
                Layer.open({
                    type: 2,
                    content:'/admin/mall_parents_order/delivery_child?id='+$(this).data('id'),
                    area:['100%','100%'],
                    //content: Config.url
                })
            })
            $(document).on('click','.refund_child' ,function (e) {
                console.log( $(this).data('id'))
                Layer.open({
                    type: 2,
                    content:'/admin/mall_parents_order/refund_child?id='+$(this).data('id'),
                    area:['60%','70%'],
                    //content: Config.url
                })
            })
            Controller.api.bindevent();
        },

        refund_child: function () {
            Controller.api.bindevent();
        },

        delivery: function () {
            Controller.api.bindevent();
        },

        delivery_child: function () {
            Controller.api.bindevent();
        },
        verification: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});